<?php
    class search_Ajax extends Ajax
    {
        public function search()
        {
            $oDatabase = new database();
            $sTypeSearch = $_POST['typeSearch'];
            if($sTypeSearch == 'User')
            {
                $sQuery = "
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
                PREFIX bus: <".URL::getLink('id/bussiness#').">

                SELECT DISTINCT ?uri ?name ?location ?email ?image     
                WHERE 
                {
                ?uri rdf:type foaf:Person.
                ?uri foaf:familyName ?name.
                ?uri bus:location ?location.
                ?uri foaf:mbox ?email.
                OPTIONAL{?uri bus:image ?image}.

                ?uri ?p ?o.
                FILTER(?p = foaf:familyName || ?p = foaf:mbox || ?p = foaf:givenName ).
                FILTER(REGEX(?o,'".$_POST['keyword']."','i')).
                }";
                $aDatas = $oDatabase->loadQuery($sQuery);
                $aResults = $aDatas['result']['rows'];
                $aParams = array(
                    '$aUsers' => $aResults
                );
                includeBlock('block/userBlock',$aParams);
            }
            else if($sTypeSearch == 'Product')
            {
                $sQuery = "
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX foaf: <http://xmlns.com/foaf/0.1/>
                PREFIX bus: <".URL::getLink('id/bussiness#').">

                SELECT DISTINCT ?uri ?name ?price ?description ?ownUser ?image        
                WHERE 
                {
                ?uri rdf:type bus:product.
                ?uri bus:name ?name.
                ?uri bus:price ?price.
                ?uri bus:defaultImage ?image.
                ?uri bus:hasManufacturer ?manufacturer.
                ?uri bus:description ?description.
                OPTIONAL{?uri bus:location ?location.}
                ?uri bus:ownUser ?userUri.
                ?userUri foaf:familyName ?ownUser.
                ?uri bus:childCategoryOf ?categoryUri.
                ?categoryUri bus:name ?categoryName.".
                (isset($_POST['sManuFacturer']) ? $_POST['sManuFacturer'] : '').
                (isset($_POST['sUserCondition']) ? $_POST['sUserCondition'] : '').
                (isset($_POST['sLocationCondition']) ? $_POST['sLocationCondition'] : '').
                (isset($_POST['sPriceCondition']) ? $_POST['sPriceCondition'] : '').
                (isset($_POST['sCategoryCondition']) ? $_POST['sCategoryCondition'] : '').
                (isset($_POST['keyword']) && !empty($_POST['keyword']) && $_POST['keyword'] != '' 
                    ? 
                    "?uri ?p ?o.     
                    FILTER(?p != bus:image).          
                    OPTIONAL
                    {
                    ?o ?u ?uo.

                    FILTER(?u != bus:description).
                    }

                    FILTER(!REGEX(?uo,'http','i') || !bound(?uo)).
                    FILTER((REGEX(?o,'".$_POST['keyword']."','i') && !REGEX(?o,'http','i')) || REGEX(?uo,'".$_POST['keyword']."','i'))." 
                    : ""
                )
                ."}";
                $aDatas = $oDatabase->loadQuery($sQuery);
                $aResults = $aDatas['result']['rows'];
                $aParams = array(
                    'aProducts' => $aResults
                );
                includeBlock('block/productBlock',$aParams);
            }
            else
            {
                $sQuery= "
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
                PREFIX bus: <".URL::getLink('id/bussiness#').">

                SELECT DISTINCT ?uri ?familyName ?name ?price ?description ?image ?categoryName ?manufacturerName ?userUri ?userName ?location 
                WHERE 
                {
                ?uri rdf:type ?type.
                OPTIONAL{?uri bus:name ?name}.
                OPTIONAL{?uri bus:defaultImage ?image}.
                OPTIONAL{?uri foaf:familyName ?familyName}.
                OPTIONAL{?uri bus:price ?price}.
                OPTIONAL{?uri bus:location ?location}.
                OPTIONAL{?uri bus:description ?description}.
                OPTIONAL
                {
                ?uri bus:hasCategory ?categoryUri.
                ?categoryUri bus:name ?categoryName.
                }.
                OPTIONAL
                {
                ?uri bus:hasManufacturer ?manufacturerUri.
                ?manufacturerUri bus:name ?manufacturerName.
                }.
                OPTIONAL
                {
                ?uri bus:ownUser ?userUri.
                ?userUri foaf:familyName ?userName.
                }.
                FILTER(?type = foaf:Person || ?type = bus:category || ?type = bus:manufacturer || ?type = bus:product).
                ?uri ?p ?o.
                FILTER(!REGEX(?o,'http','i')).
                FILTER(REGEX(?o,'".$_POST['keyword']."','i')). 
                }";
                $aDatas = $oDatabase->loadQuery($sQuery);
                $aResults = $aDatas['result']['rows'];
                $aParams = array(
                    'keyword' => $_POST['keyword'],
                    'aResultSearchs' => $aResults
                );
                includeBlock('block/searchItemBlock',$aParams);
            }
        }

        public function quickSearch()
        {
            $oDatabase = new database();
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT DISTINCT *  
            WHERE 
            {
            ?uri rdf:type ?type.
            ?uri bus:name ?name.
            ?uri bus:location ?location.
            ?uri bus:price ?price.
            ?uri bus:defaultImage ?image.
            ?uri bus:ownUser ?userUri.
            ?userUri foaf:familyName ?userName.
            FILTER(REGEX(?name,'".$_POST['keyword']."','i')).
            }
            LIMIT 6
            ";
            $aDatas = $oDatabase->loadQuery($sQuery);
            $aResults = $aDatas['result']['rows'];
            $aParams = array(
                'aProducts' => $aResults
            );
            includeBlock('block/quickSearch',$aParams);
        }
    }
?>
